package history

import (
	"fmt"
	"sync"
)

/**
 * @Description: 启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10,
				然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20....以此类推, 直到打印到75.
 * @Keyword:
 * @Author: kami
 * @Date: 2022/7/13 13:02
 **/
func takeTurnPrint() {
	var chA = make(chan int, 1)
	var chB = make(chan int, 1)
	var chC = make(chan int, 1)
	chA <- 1
	var sig sync.WaitGroup
	sig.Add(3)
	go func() {
		for {
			idx := <-chA
			if idx > 75 {
				sig.Done()
				chB <- 76
				return
			}
			var end = idx + 4
			for i := idx; i <= end; i++ {
				fmt.Println("chA: ", i)
			}
			chB <- end + 1
		}
	}()
	go func() {
		for {
			idx := <-chB
			if idx > 75 {
				sig.Done()
				chC <- 76
				return
			}
			var end = idx + 4
			for i := idx; i <= end; i++ {
				fmt.Println("chB: ", i)
			}
			chC <- end + 1
		}
	}()
	go func() {
		for {
			idx := <-chC
			if idx > 75 {
				sig.Done()
				chA <- 76
				return
			}
			var end = idx + 4
			for i := idx; i <= end; i++ {
				fmt.Println("chC: ", i)
			}
			chA <- end + 1
		}
	}()
	sig.Wait()
	fmt.Println("结束啦")
}
